<template>
  <div class="chart-container">
    <div id="three-days-flow-chart" :style="getAvailableHeight"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: 'ThreeDaysFlow',
  data() {
    return {
      screenHeight: null,
      threeDaysFlowChart: undefined,
      baseOption: {
        legend: {
          data: []
        },
        grid: {
          top: '5%',
          right: '3%',
          bottom: 65,
          left: '5%',
          containsLabel: true
        },
        tooltip:{
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        dataZoom: [{
          "show": true,
          "height": 30,
          "xAxisIndex": [
            0
          ],
          bottom: 5,
          "start": 10,
          "end": 80,
          handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
          handleSize: '110%',
          handleStyle: {
            color: "#d3dee5",

          },
          textStyle: {
            color: "#fff"
          },
          borderColor: "#90979c"
        }, {
          "type": "inside",
          "show": true,
          "height": 15,
          "start": 1,
          "end": 35
        }],
        xAxis: {
          type: 'category',
          data: [],
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: true,
          },
          axisLabel: {
            formatter: '{value} m³/h'
          }
        },
        series: [
          {
            name: '',
            data: [],
            type: 'line',
            smooth: true,
            lineStyle: {
              color: '#fa553a'
            }
          },
          {
            name: '',
            data: [],
            type: 'line',
            smooth: true,
            lineStyle: {
              color: '#12b972'
            }
          },
          {
            name: '',
            data: [],
            type: 'line',
            smooth: true,
            lineStyle: {
              color: '#0096ff'
            },
          }
        ]
      },
    }
  },
  methods: {
    initChart() {
      this.threeDaysFlowChart = echarts.init(document.getElementById('three-days-flow-chart'), 'macarons')
      this.threeDaysFlowChart.setOption(this.baseOption)
    },
    resizeCharts(height) {
      this.screenHeight = height
      this.$nextTick(() => {
        this.threeDaysFlowChart.resize()
      })
    },
    updateOption(data) {
      const timeList = Object.keys(data)
      timeList.sort()

      const option = Object.assign({}, this.baseOption)
      option.legend.data = timeList.slice(0)

      let index = 0
      const xAxisData = []
      timeList.forEach(date => {
        const dataList = data[date]
        const seriesData = []
        dataList.forEach(item => {
          xAxisData.push(item.ReadDate)
          seriesData.push([item.ReadDate, item.instantNumber])
        })
        option.series[index].name = date
        option.series[index].data = seriesData
        index += 1
      })
      option.xAxis.data = xAxisData

      this.threeDaysFlowChart.setOption(option)
    }
  },
  computed: {
    getAvailableHeight() {
      const h = this.screenHeight - 415
      return {width: '100%', height: h + 'px'}
    }
  },
  created() {
    this.screenHeight = document.body.clientHeight
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  }
}
</script>

<style scoped lang="scss">

</style>
